package com.liuwei.spring.cloud.framework.dto;


import com.liuwei.spring.cloud.common.enums.I18nEnum;
import com.liuwei.spring.cloud.common.exception.ErrorCode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

/**
 * Created by evan on 2017/1/9.
 */
@SuppressWarnings("ALL")
@Data
@Schema(description = "返回值")
public class Result<T> {

	private static final long serialVersionUID = 1L;

    @Schema(description = "是否成功")
    private boolean success;

    @Schema(description = "错误信息")
    private String message;

    @Schema(description = "错误码")
    private int code;

    @Schema(description = "返回值")
    private T data;

    @Schema(description = "提示类型，，此处主要是用于前端统一处理提示标识")
    private String reminder;

    @Schema(description = "错误级别")
    private int errorLevel;

    public Result(){}

    public Result(int code, String message){
        this.message = message;
        this.code = code;
        if(code>=20000 && code < 30001){
            this.success = true;
        }
    };

    public Result(boolean success, String message, int code){
        this.message = message;
        this.code = code;
        this.success = success;
    };

    public Result(String message, int code, T data){
        this(code,message);
        this.data = data;
    };

    public Result(boolean success, String message, int code, T data){
        this(success,message,code);
        this.data = data;
    };

    public static Result success(){
        return new Result(ErrorCode.SUCCESS.getCode(),"");
    }

    public static<T> Result success(T data){
        Result responseVO = returnSuccess();
        responseVO.setData(data);
        return responseVO;
    }
    
    private static Result returnSuccess(){
        return new Result(ErrorCode.SUCCESS.getCode(),"");
    }

    public static Result fail(){
        return returnFailure();
    }
    
    public static Result fail(I18nEnum codeEnum){
    	return new Result(codeEnum.getCode(),codeEnum.getMessage());
    }

    public static Result fail(int code, String message){
        return new Result(code,message);
    }

    public static Result fail(I18nEnum codeEnum, String message){
        return new Result(codeEnum.getCode(),message);
    }

    private static Result returnFailure(){
        return new Result(ErrorCode.SYSTEM_ERROR.getCode(),"");
    }
    
    public static Result error(I18nEnum codeEnum){
    	return new Result(codeEnum.getCode(),codeEnum.getMessage());
    }


	public static Result fail(String string) {
		return new Result(ErrorCode.SYSTEM_ERROR.getCode(),string);
	}

}
